/**
 ** remote - Open remote resources by URL
 *
 * The remote module provides functions for fetching data across the World Wide Web. 
 *
 */

define('oj/remote', [], function () {
    'use strict';
    
    var remote = {
        
        /* Functions */
        jsonp  : function (url, callbackarg, args, callback) {},
        img    : function (url, callback) {},
        script : function (url, callback) {}
        
    };
    
    
    
    var ID = (new Date).getTime(),
        TIMEOUT = 300000;
    
    
    remote.jsonp = function (url, callbackarg, args, callback) {
        /**
         *> remote.jsonp(url, callbackarg, args, callback)
         *
         * Read JSONP from URL.
         *
         *. remote.jsonp('http://jsonp.url.com/', 'callback=', 'arg1=value&arg2=value', function (msg) {
         *.     var json = JSON.parse(msg);
         *. });
         */
        
        var cb     = 'jsonp_' + String(++ID),
            timer  = setTimeout(function() {window[cb]({'error': 'timeout'});}, TIMEOUT),
            script = document.createElement('script');
        
        if (typeof args === 'undefined' || args.length === 0) {
            args = '';
        } else {
            args = '&' + args;
        }
        
        script.src = url + '?' + callbackarg + cb + args;
        
        window[cb] = function () {
            clearTimeout(timer);
            try {
                delete window[cb];
            } catch(e) {
                window[cb] = null;
            }
            if (typeof callback === 'function') {
                callback.apply(this, arguments);
            }
            document.body.removeChild(script);
        };
        
        document.body.appendChild(script);
    }
    
    
    remote.img = function (url, callback) {
        /**
         *> remote.img(url, callback)
         *
         * Read an Image from URL.
         *
         *. remote.img('http://url.com/image.jpg', function (image) {
         *.     var width = image.width;
         *. });
         */
        
        var img = document.createElement('img');
        
        img.src = '/?GI/' + encodeURIComponent(url);
        img.onload = function () {
            document.body.removeChild(img);
            if (typeof callback === 'function') {
                callback(img);
            }
        };
        
        document.body.appendChild(img);
    }
    
    
    remote.script = function (url, callback) {
        /**
         *> remote.script(url, callback)
         *
         * Load a script from URL.
         *
         *. remote.script('http://url.com/script.js')
         *
         */
        
        var script = document.createElement('script');
        
        script.src = url;
        
        script.onload = function () {
            if (typeof callback === 'function') {
                callback(script);
            }
        };
        
        document.body.appendChild(script);
    }
    
    
    return remote;
    
});
